package com.itmiaojie.service.impl;

import com.itmiaojie.pojo.ResponseResult;
import com.itmiaojie.service.UploadService;
import com.itmiaojie.utils.QiniuUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@Service
public class UploadServiceImpl implements UploadService {

    private String pre = "rk4rylr55.hd-bkt.clouddn.com/";
    @Override
    public ResponseResult uploadImg(MultipartFile img) throws IOException {
        String originalFilename = img.getOriginalFilename();
        int lastIndexOf = originalFilename.lastIndexOf(".");
        //获取文件后缀
        String suffix = originalFilename.substring(lastIndexOf - 1);
        //使用UUID随机产生文件名称，防止同名文件覆盖
        String fileName = UUID.randomUUID().toString() + suffix;
        QiniuUtils.upload2Qiniu(img.getBytes(),fileName);
        return ResponseResult.okResult("http://"+pre+fileName);
    }
}
